package com.acheli.rideable.functionLathes.component;

import com.acheli.registry.network.ACHMessages;
import com.acheli.registry.network.ACHeliPacketType;
import com.acheli.registry.network.packet.CustomSyncableC2SPacket;
import com.acheli.rideable.entites.RideableEntity;
import com.acheli.rideable.functionLathes.ComponentFL;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;
import org.mozilla.javascript.NativeObject;

public class SeatFL extends ComponentFL {
    private double mouseXDelta = 0;
    private double mouseYDelta = 0;
    private float playerYaw = 0;
    private float playerPitch = 0;
    private final double maxRotateAngle;
    public final boolean canControl;

    public static class Data extends ComponentFL.CData{
        String orientation;
        public boolean canControl = false;
        public double maxRotateAngle = 0;
    }


    public SeatFL(String ID, RideableEntity vehicle, boolean canControl, double maxRotateAngle, Orientation orientation, Vec3 relativePosition, Vec3 relativeBoxLength) {
        super(ID, vehicle, relativePosition, relativeBoxLength);
        this.orientation = orientation;
        this.canControl = canControl;
        this.maxRotateAngle = maxRotateAngle;
    }

    @Override
    public void interact(Player player, InteractionHand hand) {
        super.interact(player,hand);
        if (getBody().level().isClientSide) {
            player.startRiding(getBody());
            CompoundTag nbt = new CompoundTag();
            nbt.putBoolean("startRiding", true);
            ACHMessages.sendToServer(new CustomSyncableC2SPacket(
                    getBody().getId(), getBody().getUUID(), nbt, ACHeliPacketType.ComponentEvent));
        }
    }

    @Override
    public CompoundTag serverReceiveAndSending(CompoundTag serverOriginNbt, ServerPlayer executingPlayer, ACHeliPacketType packetType) {
        if (packetType == ACHeliPacketType.ComponentEvent) {
            if (serverOriginNbt.contains("startRiding")) {
                executingPlayer.startRiding(getBody());
            }
        }
        return super.serverReceiveAndSending(serverOriginNbt, executingPlayer, packetType);
    }

    public void setPlayerRotate(Player player, double forwardYaw, double forwardPitch) {
        double ratio = 0.1;
        double orientAngle = getOrientAngle(orientation);
        double xd = mouseXDelta * ratio;
        if (xd < -maxRotateAngle) {
            xd = -maxRotateAngle;
            mouseXDelta = xd/ ratio;
        }
        if (xd > maxRotateAngle) {
            xd = maxRotateAngle;
            mouseXDelta = xd/ ratio;
        }
        if (vehicle.level().isClientSide) {
            Vector3f euler = new Vector3f();
            totalRotation.getEulerAnglesYXZ(euler);

            playerYaw = (float) (forwardYaw + xd + orientAngle - Math.toDegrees(euler.y));
            playerPitch = (float) (forwardPitch + (mouseYDelta * ratio));
            player.setYRot(playerYaw);
            player.setXRot(playerPitch);
//            NbtCompound playerData = new NbtCompound();
//            playerData.putUuid("rotatePlayerUuid", player.getUuid());
//            playerData.putFloat("playerYaw", playerYaw);
//            playerData.putFloat("playerPitch", playerPitch);
//            ComponentEventData.setState(this, playerData);
        }
    }

    public void addMouseDelta(double deltaX, double deltaY) {
        mouseXDelta += deltaX;
        mouseYDelta += deltaY;
    }

    public void cleanMouseDelta() {
        mouseXDelta = 0;
        mouseYDelta = 0;
    }
}
